/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.tariff.controller;

import cn.iot.card.open.annotion.Permission;
import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.constants.ErrorCode;
import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.tariff.service.OrderedChargesService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 可订购资费控制器
 *
 * @author 何光鑫
 * @date 20200221 14:43:32
 * @modify 20200221 何光鑫 v1.1.1 创建
 * @since v1.1.1
 */
@Controller
@RequestMapping("/orderedCharges")
public class OrderedChargesController extends BaseController {

    /**
     * 可订购资费页面路径地址前缀
     */
    private static String PREFIX = "/modular/tariff/orderedCharges/";

    @Autowired
    private OrderedChargesService orderedChargesService;

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到可订购资费查询列表页面
     *
     * @return 返回 string
     * @author 何光鑫
     * @date 20200108 09:00:04
     * @modify 20200108 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "orderedCharges.html";
    }

    /**
     * 查询可订购资费列表
     *
     * @param accountId  集团id
     * @param status     资费类别：1-集团资费，2-群组资费，3-用户资费
     * @return 返回 object
     * @throws IOException
     * @author 何光鑫
     * @date 20200221 15:24:59
     * @modify 20200221 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String accountId, @RequestParam(required = false) String status,
                       @RequestParam(required = false) String msisdn) throws IOException{
        //当为用户资费时，通过卡号查询出accountId
        if(Constants.STATUS_USER.equals(status)){
            if(ToolUtil.isEmpty(msisdn)){
                return LayuiPageFactory.createEmptyPageInfo();
            }
            Map<String, Object> map = new HashMap<>();
            map.put(Constants.REQUEST_PARAM_MSISDN,msisdn);
            map.put(Constants.RESPONSE_DELETED,Constants.RESPONSE_DELETED_FALSE);
            List<Card> cards = cardService.getBaseMapper().selectByMap(map);
            if(ToolUtil.isEmpty(cards)){
                return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
            }
            Card card = cards.get(0);
            // 验证当前用户是否有卡权限
            LoginUser loginUser = LoginContextHolder.getContext().getUser();
            UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
            if (null == userGroupRl) {
                return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
            }
            Map<String, Object> cardDtoMap = cardService.getCardInfo(card.getCardId());
            Object accountIdObj = cardDtoMap.get(Constants.REQUEST_ACCOUNT_ID);
            if (ToolUtil.isEmpty(accountIdObj)) {
                // 当前卡未绑定任何账户
                return LayuiPageFactory.cteateErrorMsg(ErrorCode.ERR11047);
            }
            accountId = String.valueOf(accountIdObj);
        }
        String transId = CommonUtil.generateTransId(accountId);
        return orderedChargesService.queryCategories(transId,accountId,status);
    }

    /**
     * 可订购资费实时查询
     *
     * @param accountId  集团id
     * @param status     资费类别：1-集团资费，2-群组资费，3-用户资费
     * @param groupId    群组id
     * @param msisdn     卡号
     * @param catalogId  目录ID
     * @param categoryId  节点ID
     * @return 返回 object
     * @throws IOException
     * @author 何光鑫
     * @date 20200224 15:52:12
     * @modify 20200224 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/query")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String accountId, @RequestParam(required = false) String status,
                       @RequestParam(required = false) String groupId, @RequestParam(required = false) String msisdn,
                       @RequestParam(required = false) String catalogId, @RequestParam(required = false) String categoryId) throws IOException{
        return orderedChargesService.queryPurchasableOfferings(accountId,status,groupId,msisdn,catalogId,categoryId);
    }

    /**
     * 集团资费跳转到可订购资费查询页面
     *
     * @param accountId  集团id
     * @param status     资费类别：1-集团资费，2-群组资费，3-用户资费
     * @param catalogId  目录ID
     * @param categoryId  节点ID
     * @param model       
     * @return 返回 string
     * @author 何光鑫
     * @date 20200226 20:21:26
     * @modify 20200226 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/toEcOrderedCharges/{accountId}/{status}/{catalogId}/{categoryId}")
    public String toEcOrderedCharges(@PathVariable("accountId") String accountId, @PathVariable("status") String status,
                                     @PathVariable("catalogId") String catalogId, @PathVariable("categoryId") String categoryId, Model model) {
        model.addAttribute("accountId", accountId);
        model.addAttribute("status", status);
        model.addAttribute("catalogId", catalogId);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("groupId", Constants.FRONT_REQUEST_NULL);
        model.addAttribute("msisdn", Constants.FRONT_REQUEST_NULL);
        return PREFIX + "to_orderedCharges.html";
    }

    /**
     * 群组资费跳转到可订购资费查询页面
     *
     * @param accountId  集团id
     * @param status     资费类别：1-集团资费，2-群组资费，3-用户资费
     * @param groupId    群组id
     * @param catalogId  目录ID
     * @param categoryId  节点ID
     * @param model       
     * @return 返回 string
     * @author 何光鑫
     * @date 20200226 20:21:27
     * @modify 20200226 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/toGroupOrderedCharges/{accountId}/{status}/{catalogId}/{categoryId}/{groupId}")
    public String toGroupOrderedCharges(@PathVariable("accountId") String accountId, @PathVariable("status") String status,
                                        @PathVariable("catalogId") String catalogId, @PathVariable("categoryId") String categoryId, @PathVariable("groupId") String groupId, Model model) {
        model.addAttribute("accountId", accountId);
        model.addAttribute("status", status);
        model.addAttribute("catalogId", catalogId);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("groupId", groupId);
        model.addAttribute("msisdn", Constants.FRONT_REQUEST_NULL);
        return PREFIX + "to_orderedCharges.html";
    }

    /**
     * 用户资费跳转到可订购资费查询页面
     *
     * @param status     资费类别：1-集团资费，2-群组资费，3-用户资费
     * @param msisdn     卡号
     * @param catalogId  目录ID
     * @param categoryId  节点ID
     * @param model       
     * @return 返回 string
     * @author 何光鑫
     * @date 20200226 20:21:27
     * @modify 20200226 何光鑫 v1.1.1 创建
     * @since v1.1.1
     */
    @RequestMapping("/toUserOrderedCharges/{status}/{catalogId}/{categoryId}/{msisdn}")
    public String toUserOrderedCharges(@PathVariable("status") String status, @PathVariable("catalogId") String catalogId,
                                       @PathVariable("categoryId") String categoryId, @PathVariable("msisdn") String msisdn, Model model) {
        model.addAttribute("accountId", Constants.FRONT_REQUEST_NULL);
        model.addAttribute("status", status);
        model.addAttribute("catalogId", catalogId);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute("groupId", Constants.FRONT_REQUEST_NULL);
        model.addAttribute("msisdn", msisdn);
        return PREFIX + "to_orderedCharges.html";
    }
}
